﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LP580.CG;

public partial class User_UC2GiftOperate : System.Web.UI.Page
{
    private int id = Utils.RQ_int("id");
    private string act = Utils.RQ_str("act");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindData();
        bindContrl();
    }

    private void bindData()
    {
        int uid = Master.ua.UserID;
        ddlGiftType.AppendDataBoundItems = true;
        ddlGiftType.Items.Add(new ListItem("请选择", "0"));
        ddlGiftType.DataSource = clsGiftProductType.GetCollection(uid);
        ddlGiftType.DataTextField = "GiftProductTypeName";
        ddlGiftType.DataValueField = "GiftProductTypeID";
        ddlGiftType.DataBind();

        ddlPriceSpan.AppendDataBoundItems = true;
        ddlPriceSpan.Items.Add(new ListItem("请选择", "0"));
        ddlPriceSpan.DataSource = clsGiftProductPriceSpan.GetCollection();
        ddlPriceSpan.DataTextField = "GiftProductPriceSpanName";
        ddlPriceSpan.DataValueField = "GiftProductPriceSpanID";
        ddlPriceSpan.DataBind();

        //ddlGiftClass.DataSource = clsGiftProductClass.GetCollection();
        //ddlGiftClass.DataValueField = "GiftProductClassID";
        //ddlGiftClass.DataTextField = "TreeName";
        //ddlGiftClass.DataBind();
        //ddlGiftClass.Attributes.Add("size", "10");

        if (Request.UrlReferrer != null)
            hfdFromUrl.Value = Request.UrlReferrer.ToString();

        act = act.ToLower();
        if (act == "edit")
        {
            clsGiftProduct cls = new clsGiftProduct(id);
            if (cls.UserID != uid)
                Response.End();
            ltTitle.Text = "修改产品";
            
            tbxName.Text = cls.GiftProductName;
            ListItem li = ddlGiftType.Items.FindByValue(cls.GiftProductTypeID.ToString());
            if (li != null) li.Selected = true;
            li = ddlPriceSpan.Items.FindByValue(cls.GiftProductPriceSpanID.ToString());
            if (li != null) li.Selected = true;
            tbxUnit.Text = cls.GiftProductUnit;
            tbxMinOrder.Text = cls.GiftProductMinOrder.ToString();
            tbxOuterTime.Text = cls.GiftProductOuterTime.ToString();
            fckInfo.Value = cls.GiftProductInfo;
            tbxMaterial.Text = cls.GiftProductMaterial;
            tbxGiftPack.Text = cls.GiftProductPacking;
            tbxPackingNum.Text = cls.PackingNum.ToString();
            tbxGiftSize.Text = cls.GiftProductSize;
            List<clsGiftProductClass> ls = new clsGiftProductClass(cls.GiftProductClassID).GetParentCollection();
            for (int i = 0; i < ls.Count; i++)
            {
                productclass += string.Format(" dv{0}=\"{1}\"", i + 1, ls[i].GiftProductClassID);
            }
            for (int i = 0; i < 5; i++)
            {
                if (cls.GiftProductPics[i] != "")
                {
                    Image ctlImg = (Image)pnUpPic.FindControl("imgPic" + (i + 1));
                    if (ctlImg == null) continue;
                    ctlImg.Visible = true;
                    ctlImg.ImageUrl = "~/" + cls.GiftProductPics[i];
                }
            }
        }
    }
    public string productclass = "";
    private void bindContrl()
    {

    }
  
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int uid = Master.ua.UserID;

        string name = Utils.CheckSqlStr(tbxName.Text.Trim());
        if (name == "")
        {
            Utils.WrAlertGoBack("标注*号为必填不可为空！");
        }



        clsGiftProduct cls = new clsGiftProduct();

        if (act.ToLower() == "edit")
        {
            cls = new clsGiftProduct(id);
            if (cls.UserID != uid)
                Response.End();
        }
        else
        {
            cls.GiftProductPics.Add("");
            cls.GiftProductPics.Add("");
            cls.GiftProductPics.Add("");
            cls.GiftProductPics.Add("");
            cls.GiftProductPics.Add("");
        }

        cls.GiftProductName = name;
        int iorder = 0;
        if (!int.TryParse(tbxMinOrder.Text, out iorder))
            iorder = 0;
        cls.GiftProductMinOrder = iorder;

        int iTmp = 0;
        if (!int.TryParse(Request["giftclass"], out iTmp))
            iTmp = 0;
        cls.GiftProductClassID = iTmp;
        iTmp = 0;
        if (!int.TryParse(ddlPriceSpan.SelectedValue, out iTmp))
            iTmp = 0;
        cls.GiftProductPriceSpanID = iTmp;
        iTmp = 0;
        if (!int.TryParse(ddlGiftType.SelectedValue, out iTmp))
            iTmp = 0;
        cls.GiftProductTypeID = iTmp;
        cls.GiftProductUnit = Utils.CheckSqlStr(tbxUnit.Text);
        cls.GiftProductInfo = Utils.CheckSqlStr(fckInfo.Value);
        DateTime dt = new DateTime(1900, 1, 1);
        int.TryParse(tbxOuterTime.Text, out iTmp);
        cls.GiftProductOuterTime = iTmp;
        cls.GiftProductMaterial = Utils.CheckSqlStr(tbxMaterial.Text);
        cls.GiftProductPacking = Utils.CheckSqlStr(tbxGiftPack.Text);
        int.TryParse(tbxPackingNum.Text.Trim(), out iTmp);
        cls.PackingNum = iTmp;
        cls.GiftProductSize = Utils.CheckSqlStr(tbxGiftSize.Text);
        string strRootPath = Page.MapPath("../upfiles/" + cls._FileSaveDir);

        for (int i = 1; i < 6; i++)
        {
            string strFilenamePath = "";
            string strFileExt = "";
            FileUpload ctlFup = (FileUpload)pnUpPic.FindControl("fupPic" + i);
            if (ctlFup == null) continue;
            if (ctlFup.HasFile)
            {
                strFileExt = System.IO.Path.GetExtension(ctlFup.FileName).ToLower();
                if (strFileExt != ".jpg" && strFileExt != ".gif" && strFileExt != ".png" && strFileExt != ".bmp")
                {
                    Response.Write("<script>alert('提示：\\n\\n格式有问题，只技持jpg|gif|png|bmp格式文件哦！');history.go(-1);</script>");
                    Response.End();
                }
                else
                {
                    strFilenamePath = Utils.MakeRndName(strRootPath) + "_" + i + strFileExt;

                    ctlFup.SaveAs(strRootPath + "\\" + strFilenamePath);

                    if (cls.GiftProductPics[i-1] != "") clsGiftProduct.DelFile(cls.GiftProductPics[i-1]);

                    cls.GiftProductPics[i-1] = "upfiles/" + cls._FileSaveDir + "/" + strFilenamePath;
                }
            }
        }
        if (act.ToLower() == "edit")
        {
            clsGiftProduct.Update(cls);
        }
        else
        {
            cls.UserID = uid;
            cls.GiftProductAddTime = DateTime.Now;
            cls.GiftProductHits = 0;
            clsGiftProduct.Insert(cls);
        }


        string goUrl = hfdFromUrl.Value;
        if (goUrl == "")
        {
            goUrl = "UC2Gift.aspx";
        }
        Utils.WrAlertGo("操作成功！", goUrl);
    }
}